package itbour.onetouchshow.activity.searchlist;

import com.google.gson.Gson;
import com.lsjr.callback.EncryBeanCallBack;
import com.lsjr.utils.HttpUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import itbour.onetouchshow.base.mvp.BasePresenterImpl;
import itbour.onetouchshow.bean.NoDataResult;
import itbour.onetouchshow.constants.AppConfig;
import itbour.onetouchshow.constants.AppConst;
import itbour.onetouchshow.constants.platform.SearchPlatform;
import itbour.onetouchshow.utils.L_;

/**
 * MVPPlugin
 * 邮箱 784787081@qq.com
 */

public class SearchListPresenter extends BasePresenterImpl<SearchListContract.View> implements SearchListContract.Presenter {
//    public void searchInfo(SearchListFragment fragment, int orderBy) {
//
//    }
//
//
//    public void searchInfo(SearchListFragment fragment, String keyWord) {
//
//    }


    private List mtypes = new ArrayList();

    public SearchListPresenter(SearchListContract.View view) {
        this.mvpView = view;

    }

    public void loadSearch(SearchListFragment searchListActivity, String keyWord, int orderBy, String tmplTypes, String durations) {
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("device", AppConst.ANDROID_DEVICE);
        // stringObjectHashMap.put("userId", App.getUserId());
        //pageNum	是
        stringObjectHashMap.put("pageNum", searchListActivity.mPageIndex);
        //页码 pageSize
        stringObjectHashMap.put("pageSize", AppConst.PAGE_SIZE);
        //   页容量
        stringObjectHashMap.put("orderBy", orderBy);
        stringObjectHashMap.put("showTypes", new Gson().toJson(mtypes));
        stringObjectHashMap.put("keyword", keyWord);
        if (durations != null) {
            stringObjectHashMap.put("durations", durations);
        }
        if (tmplTypes != null) {
            stringObjectHashMap.put("tmplTypes", tmplTypes);
        }
        stringObjectHashMap.put("withTotalPage", 1);
        L_.e("keywork----" + keyWord + "--orderBy---" + orderBy + "---tmplTypes" + tmplTypes + "---durations" + durations + "showTypes" + new Gson().toJson(mtypes));
        HttpUtils.getInstance().executeGet(AppConfig.SEARCHTEMPLETE_V1_0, stringObjectHashMap, new EncryBeanCallBack() {
            @Override
            protected void onXError(String exception) {
                if (mvpView != null) {
                    mvpView.loadFaild("网络异常");
                }

            }

            @Override
            protected void onSuccess(String response) {
                NoDataResult noDataResult = new Gson().fromJson(response, NoDataResult.class);

                if (mvpView != null) {
                    if (noDataResult.getList() == null || noDataResult.getList().size() == 0) {
                        mvpView.loadFaild("没有数据");
                    } else {
                        mvpView.loadSucceed(response);
                    }

                }

                L_.e(response);
            }
        });
    }

    public void loadSearch(SearchListFragment searchListActivity, String keyWord, int orderBy, int mtype, String tmplTypes, String durations) {
        List mIds = new ArrayList();
        switch (mtype) {
            case SearchPlatform.TYPE_ALL:
                mIds.addAll(AppConst.mSearchIds);
                break;
            default:
                mIds.add(mtype);
                break;
        }
        mtypes = mIds;
        loadSearch(searchListActivity, keyWord, orderBy, tmplTypes, durations);
    }
}
